package com.example.harmonyosproject.server.controller;
import com.example.harmonyosproject.Utils.JwtUtils;
import com.example.harmonyosproject.pojo.PasswordChangeRequest;
import com.example.harmonyosproject.pojo.Result;
import com.example.harmonyosproject.pojo.User;
import com.example.harmonyosproject.server.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;


//    注册
//    {
//        "phone":"44321",
//            "password":"123456",
//            "name":"王五",
//            "image":"王五image"
//    }
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        log.info(user.toString());
        Boolean reg=userService.register(user);
        if(reg)
        return Result.success();
        else
            return Result.error("账号已存在");
    }
//    通过手机号获取个人信息
//    路径传参,/phone
//返回参数：
//{
//    "code": 1,
//        "msg": "success",
//        "data": {
//    "id": 6,
//            "username": "张三",
//            "password": "123",
//            "name": "张三",
//            "phone": "123456",
//            "gender": null,
//            "image": "image2",
//            "createTime": "2024-07-07T13:44:00"
//}
//}
    @GetMapping("/getByPhone/{phone}")
    public Result getByPhone(@PathVariable String phone){
//        log.info(phone);
        User user = userService.getByPhone(phone);
        return Result.success(user);
    }
//    通过用户id获取个人信息
//    路径传参,/id
//返回参数：
//{
//    "code": 1,
//        "msg": "success",
//        "data": {
//    "id": 6,
//            "username": "张三",
//            "password": "123",
//            "name": "张三",
//            "phone": "123456",
//            "gender": null,
//            "image": "image2",
//            "createTime": "2024-07-07T13:44:00"
//}
//}
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Integer id){
//        log.info(phone);
        User user = userService.getById(id);
        return Result.success(user);
    }

//    登入
//    {
//        "phone":"123456",
//         "password":"123"
//    }

//返回参数：
//{
//    "code": 1,
//        "msg": "success",
//        "data": "eyJhbGciOiJIUzI1NiJ9.eyJpbWFnZSI6ImltYWdlMiIsInBob25lIjoiMTIzNDU2IiwibmFtZSI6IuW8oOS4iSIsImlkIjo2LCJ1c2VybmFtZSI6IuW8oOS4iSIsImV4cCI6MTcyMDQ0NTY0NX0.p2pvMP2yKiT70fNpl7DOAyVB3W2XXCfZtWadbTnlKLs"
//}
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        log.info(user.toString());
        User user1 =userService.login(user);
        if(user1!=null){
            Map<String, Object> claims=new HashMap<>();
            claims.put("id",user1.getId());
            claims.put("phone",user1.getPhone());
            claims.put("name",user1.getName());
            claims.put("username",user1.getUsername());
            claims.put("image",user1.getImage());
            String jwt=JwtUtils.generateJwt(claims);
            user1.setToken(jwt);
            return Result.success(user1);
        }
        else
            return Result.error("用户名或密码错误");
    }
//    修改个人信息 可修改：名字：name,头像：image,性别:gender,微信号：username
//    {
//        "id":6,
//            "image":"image2"
//    }
    @PutMapping("/changeInfo")
    public Result changeInfo(@RequestBody User user){
        userService.changeInfo(user);
        return Result.success();
    }
//    修改密码
//    {
//        "id":6,
//            "oldPassword":"123456",
//            "newPassword":"123"
//    }
    @PutMapping("/changePassword")
    public Result changePassword(@RequestBody PasswordChangeRequest passwordChangeRequest){
        log.info(passwordChangeRequest.toString());
        log.info("ok");
        Boolean bl=userService.changePassword(passwordChangeRequest);
        if(bl)
        return Result.success();
        else
            return Result.error("原密码错误");
    }



}
